package com.freshfresh.activity.classification;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.R;
import com.freshfresh.adapter.FenleiAdapter;
import com.freshfresh.utils.RequestManager;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
import com.hangyjx.util.AndroidUtil;
//分类
public class ClassificationActivity extends Activity {
	private ImageView iv_back = null;
	private GridView gridView = null;
	Dialog progressDialog;
	List<Map<String,Object>> listmap;
	FenleiAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ac_classification);
		initView();
		
		String str_imei=AndroidUtil.getMei(ClassificationActivity.this);
		
		Log.e("imei是多少…………", str_imei);
		
		if (progressDialog == null) {
			progressDialog = Utils.createLoadingDialog(ClassificationActivity.this,
					"数据加载中……");
		}
		progressDialog.show();

		Map<String, String> params = new HashMap<String, String>();
		String ad_url = UrlConstants.fenlei;
		Log.e("url是多少…………", ad_url);
		executeRequest(new StringRequest(ad_url, params,
				new Response.Listener<String>() {
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						Log.e("listmap的……", arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							Map<String,Object> map=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
							listmap=(List<Map<String,Object>>)map.get("category_list");
							adapter=new FenleiAdapter(ClassificationActivity.this, listmap);
							gridView.setAdapter(adapter);
							gridView.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?> arg0,
										View arg1, int arg2, long arg3) {
									Intent intent=new Intent(ClassificationActivity.this,ExclusiveActivity.class);
									intent.putExtra("id", listmap.get(arg2).get("category_id").toString());
									startActivity(intent);
								}
								
							});
						} else {
							
						}
					}

				}, new Response.ErrorListener() {

					public void onErrorResponse(VolleyError arg0) {
						// dialog.dismiss();
						Toast.makeText(ClassificationActivity.this, "获取失败", 1).show();
						progressDialog.dismiss();
					}
				}));
	}

	public void initView() {
		listmap=new ArrayList<Map<String,Object>>();
		iv_back = (ImageView) findViewById(R.id.iv_back);
		gridView = (GridView) findViewById(R.id.gridView);
		iv_back.setVisibility(View.GONE);
	}

	protected void executeRequest(Request request) {
		RequestManager.addRequest(request, this);
	}
}
